package com.lgq.account.controller;

import com.lgq.account.client.AuthServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.lgq.common.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lgq.account.service.UserService;
import com.lgq.common.entity.DatatablesJSON;
import com.lgq.common.entity.JSONResult;
import com.lgq.account.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.Principal;

/**
 *code is far away from bug with the animal protecting
 *  ┏┓　　　┏┓
 *┏┛┻━━━┛┻┓
 *┃　　　　　　　┃ 　
 *┃　　　━　　　┃
 *┃　┳┛　┗┳　┃
 *┃　　　　　　　┃
 *┃　　　┻　　　┃
 *┃　　　　　　　┃
 *┗━┓　　　┏━┛
 *　　┃　　　┃神兽保佑
 *　　┃　　　┃代码无BUG！
 *　　┃　　　┗━━━┓
 *　　┃　　　　　　　┣┓
 *　　┃　　　　　　　┏┛
 *　　┗┓┓┏━┳┓┏┛
 *　　　┃┫┫　┃┫┫
 *　　　┗┻┛　┗┻┛
 *　　
 *   @description : User 控制器
 *   ---------------------------------
 * 	 @author Liang.Guangqing
 *   @since 2017-11-03
 */
@RestController
@RequestMapping("/user")
@Api(value="用户管理", description="用户管理 控制器")
public class UserController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    public UserService userService;

    @Autowired
    private AuthServiceClient authServiceClient;

    /**
     * @description : 获取分页列表
     * ---------------------------------
     * @author : Liang.Guangqing
     * @since : Create in 2017-11-03
     */
    @GetMapping("/getUserList")
    @ApiOperation(value="/getExpireList", notes="获取用户列表(分页)")
    public Object getUserList(User param , @Value("0") Integer draw, Integer length, Integer start) {
            DatatablesJSON<User> resJson=new DatatablesJSON<>();
            try {
                Integer pageNo=getPageNo(start,length);
                Page<User> page=new Page<User>(pageNo,length);
                userService.selectPageWithParam(page, param);
                resJson.setDraw(draw++);
                resJson.setRecordsTotal(page.getTotal());
                resJson.setRecordsFiltered(page.getTotal());
                resJson.setData(page.getRecords());
                resJson.setSuccess(true);
            }catch (Exception e){
                resJson.setSuccess(false);
                resJson.setError("异常信息:"+e.getClass().getName());
                logger.info("异常信息:{}",e.getMessage());
            }
            return resJson;
    }

    /**
     * @description : 通过id获取User
     * ---------------------------------
     * @author : Liang.Guangqing
     * @since : Create in 2017-11-03
     */
    @GetMapping("/getUserById")
    public Object getUserById(String id) {
            JSONResult<User> resJson = new JSONResult<>();
            try {
                User param= userService.selectOneByObj(id);
                resJson.setData(param);
                resJson.setSuccess(true);
            }catch (Exception e) {
                resJson.setSuccess(false);
                resJson.setMessage("异常信息:"+e.getClass().getName());
                logger.info("异常信息:{}",e.getMessage());
            }
            return resJson;
    }

    /**
     * @description : 通过id删除User
     * ---------------------------------
     * @author : Liang.Guangqing
     * @since : Create in 2017-11-03
     */
    @PostMapping("/deleteUserById")
    public Object deleteUserById(String id) {
            JSONResult<User> resJson = new JSONResult<>();
            try{
                resJson.setSuccess(userService.deleteById(id));
            }catch (Exception e) {
                resJson.setSuccess(false);
                resJson.setMessage("异常信息:"+e.getClass().getName());
                logger.info("异常信息:{}",e.getMessage());
            }
            return resJson;
    }

    /**
     * @description : 通过id更新User
     * ---------------------------------
     * @author : Liang.Guangqing
     * @since : Create in 2017-11-03
     */
    @PostMapping("/updateUserById")
    public Object updateUserById(User param) {
            JSONResult<User> resJson = new JSONResult<>();
            try{
                resJson.setSuccess(userService.updateById(param));
            }catch (Exception e) {
                resJson.setSuccess(false);
                resJson.setMessage("异常信息:"+e.getClass().getName());
                logger.info("异常信息:{}",e.getMessage());
            }
            return resJson;
    }

    /**
     * @description : 添加User
     * ---------------------------------
     * @author : Liang.Guangqing
     * @since : Create in 2017-11-03
     */
	@PostMapping("/addUser")
    public Object addUser(User param) {
            JSONResult<User> resJson = new JSONResult<>();
            try{
                resJson.setSuccess(userService.insert(param));
            }catch (Exception e) {
                resJson.setSuccess(false);
                resJson.setMessage("异常信息:"+e.getClass().getName());
                logger.info("异常信息:{}",e.getMessage());
            }
            return resJson;
    }

    @GetMapping("userDetail")
    public Object userDetail() {
        return (OAuth2AuthenticationDetails) SecurityContextHolder
                .getContext().getAuthentication().getDetails();
    }


    @GetMapping("/createUser")
    public Object createUser(User user) {
	    authServiceClient.createUser(user);
	    return null;
    }
}
